<!-- 费用报销 -->
<template>
	<view>
		<u-sticky>
			<u-tabs
				class="tabs" 
				:list="tabsList" 
				@click="onTabClick"
				:scrollable="false" style="padding: 0 24rpx;background: #FFF;z-index:22;position: sticky;top: 0px;"
				:activeStyle="{color:'#171717', fontSize: '14px'}"
				:inactiveStyle="{color:'#C6C6C6', fontSize: '14px'}"
				:itemStyle="{ whiteSpace:'nowrap',height: '44px' }"
				lineColor="#1E6FFF"
			></u-tabs>
		</u-sticky>
		<view class="task_lists" :style="{ paddingBottom: tabvalue == '待办'?'130rpx':'0' }">
			<collapse name="基本信息" id="outside1" :maxHeight="maxHeight">
				<view class="listdefault" :id="uId">
					
					<view class="action" v-for="(item,key) in basicInfo" :key="key">
						<block v-if="item == 'settleType_dictText'">
							<view class="action" v-if="detailData.isPay == '1'">
								<view class="list1">{{key}}</view>
								<view class="list2">{{ detailData[item] }}</view>
							</view>
						</block>
						<block v-else>
							<view class="list1">{{ key }}</view>
							<view class="list2">{{ detailData[item] }}</view>
						</block>
					</view>
					<block v-if="detailData.feeCategory == '2'">
						<view class="action" >
							<view class="list1">是否关联项目</view>
							<view class="list2">{{ detailData.isRelProject_dictText }}</view>
						</view>
					</block>
					
					<!-- 结算方式 0 - 转账 1- 现金 2 - 冲账 3 - 其他 -->
					<block v-if="detailData.settleType=='0'">
						<view class="action" v-if="detailData.isPay == 1">
							<view class="list1">转账类型</view>
							<view class="list2">{{ detailData.transferType_dictText }}</view>
						</view>
						<view class="action" v-if="detailData.isPay == 1 && detailData.transferType">
							<view class="list1">收款户名</view>
							<view class="list2">{{ detailData.accountName_dictText }}</view>
						</view>
						<view class="action" v-if="detailData.isPay == 1 && detailData.transferType && detailData.accountName">
							<view class="list1">收款方开户行</view>
							<view class="list2">{{ detailData.bankName }}</view>
						</view>
						<view class="action" v-if="detailData.isPay == 1 && detailData.transferType && detailData.accountName">
							<view class="list1">收款方银行账户</view>
							<view class="list2">{{ detailData.accountNo }}</view>
						</view>
					</block>
					
					<!-- 选择冲账单据类型 -->
					<block v-if="detailData.relBillType">
						<view class="action">
							<view class="list1">关联单据单号</view>
							<view class="list2">{{ relBillId_dictText }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 1">
							<view class="list1">借款金额(元)</view>
							<view class="list2">{{ detailData.borrowLoanAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 1">
							<view class="list1">已还金额(元)</view>
							<view class="list2">{{ detailData.borrowOnloanAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 1">
							<view class="list1">未还金额(元)</view>
							<view class="list2">{{ detailData.borrowUnloanAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 1">
							<view class="list1">剩余未付金额(元)</view>
							<view class="list2">{{ detailData.borrowPayAmount }}</view>
						</view>
						
						<view class="action" v-if="detailData.relBillType == 2">
							<view class="list1">预付金额(元)</view>
							<view class="list2">{{ detailData.payPayableAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 2">
							<view class="list1">已冲账金额(元)</view>
							<view class="list2">{{ detailData.payOnChargedAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 2">
							<view class="list1">未冲账金额(元)</view>
							<view class="list2">{{ detailData.payUnChargedAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 3">
							<view class="list1">保证金总额(元)</view>
							<view class="list2">{{ detailData.depositRemittanceAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 3">
							<view class="list1">服务费(元)</view>
							<view class="list2">{{ detailData.depositServiceCharge }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 3">
							<view class="list1">利息金额(元)</view>
							<view class="list2">{{ detailData.depositInterestAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 3">
							<view class="list1">归还金额(元)</view>
							<view class="list2">{{ detailData.depositReturnAmount }}</view>
						</view>
						<view class="action" v-if="detailData.relBillType == 3">
							<view class="list1">未还金额(元)</view>
							<view class="list2">{{ detailData.depositUnreturnAmount }}</view>
						</view>
					</block>
				</view>
			</collapse>
			
			<action-collapse 
				name="费用报销信息" 
				v-if="detailData.feeCategory == '0'" 
				:childFields="childFields1" 
				id="outside2" 
				:dataSource="childArr"
			></action-collapse>
			<!-- 采购类 -->
			<collapse name="费用报销信息" id="outside2" :maxHeight="maxHeight1" v-if="detailData.feeCategory == '1'">
				<view class="listdefault" :id="uId1">
					<view class="action">
						<view class="list1">项目名称</view>
						<view class="list2" style="color: #1E6FFF;" @click="onClickProjectName(childObj)">{{ childObj.projectId_dictText }}</view>
					</view>
					<view class="action" v-for="(item,key) in childFields2" :key="key">
						<view class="list1">{{ key }}</view>
						<view class="list2">{{ childObj[item] }}</view>
					</view>
				</view>
			</collapse>
			<!-- 费用类 -->
			<action-collapse name="费用报销信息" v-if="detailData.feeCategory == '2'" :childFields="detailData.isRelProject == 0?childFields4:childFields3" id="outside2" :dataSource="childSource">
				<span slot="projectId_dictText" slot-scope="projectId" style="color: #1E6FFF;" @click="onClickProjectName(projectId.record)">{{projectId.text}}</span>
			</action-collapse>
			
			<default-list name="附件">
				<file-list :fileList="detailData.attachments"></file-list>
			</default-list>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	import actionCollapse from '@/pages/synergy/components/actionCollapse.vue'
	import detailInformationList from '@/pages/synergy/components/detailInformationList.vue'
	import fileList from '@/pages/synergy/components/fileList.vue'
	import collapse from '@/pages/synergy/components/collapse.vue'
	import defaultList from '@/pages/synergy/components/defaultList.vue'
	import flowList from '@/pages/synergy/components/flowList.vue'
	import {
		queryByIdreimbursementBill, // 根据id查询信息
		getChildrenListSalary,
		getChildrenListDaily,
		getChildrenListPurchasey,
		queryByIdFinanceLoanBill,
		queryByIdFinancePaymentSlipFeeDetail,
		queryByIdFinanceProjectDepositBill,
		erpEmployeeFileQueryFileById ,// 姓名查询
		erpOmcContractManagerFinalQueryById,
		erpProgressPaymentNodeQueryByIds
	} from '@/api/index.js'
	import {
		BASE_URL
	} from '@/utils/baseUrl.js'
	export default {
		data() {
			return {
				relBillId_dictText: '', // 费用报销类型
				tabsList: [
					{ name: '基本信息',id: 'outside1' },
					{ name: '费用报销信息',id: 'outside2' },
				],
				detailData: {}, // 详情数据
				basicInfo: { // 主表信息字段
					'单据编号': 'billNo',
					'填表时间': 'createTime',
					'费用所属团队': 'teamId_dictText',
					'申请公司': 'applicant_dictText',
					'费用所属年度': 'teamYear_dictText',
					'报销人': 'proposer_dictText',
					'所在部门': 'proposerDeptId_dictText',
					'团队费用类别': 'teamFeeCategory_dictText',
					'单据类型': 'billCategory_dictText',
					'本次是否付款': 'isPay_dictText',
					'本次付款金额(元)': 'payAmount',
					'累计报销金额(元)': 'accumulateAmount',
					'报销说明': 'applyExplain',
					'费用类别': 'feeCategory_dictText',
					'结算方式': 'settleType_dictText',
					'冲账单据类型': 'relBillType_dictText',
				},
				childFields1: {
					'工资福利大类': 'bigCategory_dictText',
					'工资福利小类': 'smallCategory_dictText',
					'发票类型': 'isSpecial_dictText',
					'报销金额(元)': 'amount',
					'税费金额(元)': 'taxation',
					'未税金额(元)': 'untaxedAmount',
				}, // 子表字段
				childFields2: {
					'采购大类': 'bigCategory_dictText',
					'采购小类': 'smallCategory_dictText',
					'关联采购合同': 'relContractId_dictText',
					'发票类型': 'isSpecial_dictText',
					'合同款项': 'contractPaymentId_dictText',
					'应付款金额(元)': 'paymentAmount',
					'已付金额(元)': 'collectedAmount',
					'未付金额（含扣款)(元)': 'unpaymentAmount',
					'报销金额(元)': 'amount',
					'税费金额(元)': 'taxation',
					'未税金额(元)': 'untaxedAmount',
				}, 
				// 选择是关联项目 - 费用类列表
				childFields3: {
					'项目名称': 'projectId_dictText',
					'合同名称': 'relContractId_dictText',
					'任务分类': 'taskCategory_dictText',
					'任务说明': 'taskName',
					'业绩归属年度': 'performanceAttributionYear',
					'费用大类': 'bigCategory_dictText',
					'费用小类': 'smallCategory_dictText',
					'发票类型': 'isSpecial_dictText',
					'报销金额(元)': 'amount',
					'税费金额(元)': 'taxation',
					'未税金额(元)': 'untaxedAmount',
				}, 
				// 选择否关联项目 - 费用类列表
				childFields4: {
					'费用大类': 'bigCategory_dictText',
					'费用小类': 'smallCategory_dictText',
					'发票类型': 'isSpecial_dictText',
					'报销金额(元)': 'amount',
					'税费金额(元)': 'taxation',
					'未税金额(元)': 'untaxedAmount',
				}, // 子表字段
				resultList: [], // 流程图数据
				nodePositionInfo: {}, // 坐标信息
				processInstanceId: '',
				scrollTop: 0, // 滚动顶部距离
				model: {}, // 处理单据表信息
				resultObj: {}, // 流程图流转信息
				nodeObj: {}, // 流程节点信息
				tabvalue: '', // 协同首页tabs name
				childSource: [], // 子表数据
				childObj: {},
				childArr: [],
				isClick: false,
				maxHeight1: 0,
				maxHeight0: 0,
				maxHeight: 0,
				uId0: uni.$u.guid(),
				uId: uni.$u.guid(),
				uId1: uni.$u.guid()
			}
		},
		components: {
			actionCollapse,
			detailInformationList,
			fileList,
			collapse,
			defaultList,
			flowList
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onLoad(options) {
			this.id = options.id || ''
			this.loadData(this.id)
		},
		methods: {
			// 查询姓名
			getNameDetail (id) {
				erpEmployeeFileQueryFileById({id}).then(res => {
					if (res.success) {
						this.detailData.accountName_dictText = res.result.name
					}
				})
			},
			// 查询预立项申请详情
			async loadData(id){
				queryByIdreimbursementBill({ id }).then( async res => {
					if(res.success){
						this.detailData = await res.result
						this.relBillType() // 查询费用类型关联单据编号
						// 查询姓名
						if (res.result.transferType == 1) {
							this.getNameDetail(res.result.accountName)
						}
						if(res.result.feeCategory == 0){
							this.getChildData2(getChildrenListSalary,res.result.id)
							// this.$nextTick(function() {
							// 	this.getMain('maxHeight0','uId0')
							// })
						}
						if(res.result.feeCategory == 1){
							this.getChildData1(getChildrenListPurchasey,res.result.id)
							// this.$nextTick(function() {
							// 	this.getMain('maxHeight1','uId1')
							// })
						}
						if(res.result.feeCategory == 2){
							this.getChildData(res.result.id)
						}
						this.timer = setTimeout(() => {
						  this.$nextTick(() => {
						  	uni.createSelectorQuery().select('#'+this.uId).boundingClientRect(data => {
						  		this.maxHeight = data.height
						  	}).exec();
						  })
							if(res.result.feeCategory == 0){
								this.$nextTick(() => {
									this.getMain('maxHeight0','uId0')
								})
							}
							if(res.result.feeCategory == 1){
								this.$nextTick(() => {
									this.getMain('maxHeight1','uId1')
								})
							}
						  clearTimeout(this.timer)
						  this.timer = null
						}, 500)
					} else {
						this.$refs.uToast.show({
							message: res.message,
							type: 'error'
						})
					}
				})
			},
			// 查詢单据编号
			relBillType () {
				var queryById = ''
				var params = ''
				if (this.detailData.relBillType == 1) {
					queryById = queryByIdFinanceLoanBill
					params = {
						id: this.detailData.relBillId
					}
				} else if (this.detailData.relBillType == 2) {
					queryById = queryByIdFinancePaymentSlipFeeDetail
					params = {
						id: this.detailData.relBillId
					}
				} else if (this.detailData.relBillType == 3) {
					queryById = queryByIdFinanceProjectDepositBill
					params = {
						id: this.detailData.relBillId
					}
				}
				if (!queryById) {
					return false
				}
				queryById(params).then(res => {
					if(res.success){
						this.relBillId_dictText = res.result.billNo
						if (this.detailData.relBillType == 2) {
							this.relBillId_dictText = res.result.billNo
						} else {
							this.relBillId_dictText = res.result.billNo
						}
					} else {
						this.$refs.uToast.show({
							message: res.message,
							type: 'error'
						})
					}
				})
				
			},
			// 查询内容高度
			queryRect(id) {
				// #ifndef APP-NVUE
				// $uGetRect为uView自带的节点查询简化方法，详见文档介绍：https://www.uviewui.com/js/getRect.html
				// 组件内部一般用this.$uGetRect，对外的为uni.$u.getRect，二者功能一致，名称不同
				return new Promise(resolve => {
					this.$uGetRect(`#${this[id]}`).then(size => {
						resolve(size)
					})
				})
				// #endif
			},
			async getMain(key,id){
				const rect = await this.queryRect(id)
				this[key] = rect.height
			},
			// 查询报销子表数据
			getChildData(id){
				getChildrenListDaily({ billId: id }).then(res => {
					if(res.success){
						this.childSource = res.result.records || res.result
					}
				})
			},
			// 查询报销子表数据
			getChildData1(url,id){
				url({ billId: id }).then(res => {
					if(res.success){
						this.childObj = res.result || {}
						if (res.result && res.result.relContractId && res.result.projectId && res.result.projectId_dictText
) {
							this.getErpOmcContractManagerFinalQueryById(res.result.relContractId)
						}
						if (res.result && res.result.contractPaymentId && res.result.projectId) {
							this.getErpProgressPaymentNodeQueryByIds(res.result.contractPaymentId)
						}
						if (!res.result.projectId) {
							if (res.result.contractPaymentId_dictText) {
								var arr = res.result.contractPaymentId_dictText.split(',')
								var name = ''
								arr.map(value => {
									if (value == 1) {
										name = '预付款' + ',' + name
									}
									if (value == 2) {
										name = '进度款' + ',' + name
									}
									if (value == 3) {
										name = '尾款' + ',' + name
									}
								})
								name = name.substr(0, name.length - 1)
							}
							this.childObj.contractPaymentId_dictText = name
						}
					}
				})
			},
			// 查询关联采购合同详情
			getErpOmcContractManagerFinalQueryById (id) {
				erpOmcContractManagerFinalQueryById({id}).then(res => {
					if (res.success) {
						this.childObj.relContractId_dictText = res.result.htmc
					}
				})
			},
			// 查询合同款项详情
			getErpProgressPaymentNodeQueryByIds (ids) {
				erpProgressPaymentNodeQueryByIds({ids}).then(res => {
					if (res.success) {
						var name = ''
						if (res.result && res.result.length) {
							res.result.map(value => {
								name = value.nodeName + ',' + name
							})
						}
						name = name.slice(0, name.length - 1)
						this.childObj.contractPaymentId_dictText = name
					}
				})
			},
			
			getChildData2(url,id){
				url({ billId: id }).then(res => {
					if(res.success){
						this.childArr = res.result || []
					}
				})
			},
			// 点击tabs
			onTabClick (item) {
				uni.createSelectorQuery().in(this).select(`#${item.id}`).boundingClientRect(data => {
					uni.pageScrollTo({
						scrollTop:data.top + this.scrollTop - 54,
						duration: 150
					})
				 }).exec();
			},
			// 点击项目名称
			onClickProjectName (item) {
				uni.redirectTo({
					url: '/pages/reportFilesList/erpProjectReportListDetail?id=' + item.projectId
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
@import "@/pages/synergy/common/action.scss";
.task_lists{
	padding: 20rpx 20rpx 0;
}
.image_lists{
	padding: 0 0 12rpx 12rpx;
	display: flex;
	flex-wrap: wrap;
	.image_list{
		margin-bottom: 20rpx;
		width: 144rpx;
		margin-right: 14rpx;
		.image_img{
			width: 116rpx;
			height: 116rpx;
			padding: 12rpx;
			background: rgba(0,0,0,0.0200);
			border-radius: 1px;
			border: 1px solid rgba(0,0,0,0.0800);
			position: relative;
			.image_action{
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translateX(-50%) translateY(-50%);
				width: 36rpx;
				height: 36rpx;
			}
			.image_source{
				width: 100%;
				height: 100%;
			}
		}
		.image_name{
			font-size: 10px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #B2B2B2;
			line-height: 28rpx;
			margin-top: 8rpx;
			text-align: left;
			overflow:hidden;
			white-space:nowrap;
			text-overflow:ellipsis;
			width: 152rpx;
		}
	}
}

.flow_right{
	display: flex;
	align-items: center;
	image{
		width: 40rpx;
		height: 40rpx;
	}
	text{
		font-size: 14px;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #1E6FFF;
		line-height: 40rpx;
		margin-left: 4rpx;
	}
}

.task_bottom{
	display: flex;
	align-items: center;
	padding: 20rpx 0 20rpx 52rpx;
	background-color: #FFFFFF;
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	z-index: 10000;
	.task_bottom_write{
		display: flex;
		align-items: center;
		image{
			width: 48rpx;
			height: 48rpx;
			margin-right: 8rpx;
		}
		text{
			font-size: 14px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #7F7F7F;
			line-height: 40rpx;
		}
	}
	.refuse{
		width: 168rpx;
		height: 90rpx;
		line-height: 90rpx;
		background: rgba(0,0,0,0.0500);
		border-radius: 10rpx;
		margin: 0 16rpx 0 52rpx;
		font-size: 17px;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(0,0,0,0.5000);
		text-align: center;
	}
	.agree{
		width: 318rpx;
		height: 90rpx;
		line-height: 90rpx;
		background: #1E6FFF;
		border-radius: 10rpx;
		font-size: 17px;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		text-align: center;
	}
}
</style>
